<?php
/**
 * @name CDataXmlStore
 * @version 1.0
 * @package lib
 * @subpackage data
 * @deprecated
 * portal.xml==>
 * <settings>
	<title>Vimua.com</title>
	<no-ftp>true</no-ftp> ==> Thống nhất không sử dụng FTP
	<ftp>
		<user>login</user>
		<password>123456</password>
		<host>192.168.1.47</host>
		<file-server-url>http://192.168.1.47/</file-server-url>
		<http-resource>http://192.168.1.47/</http-resource>
	</ftp>
</settings>
 * mà sẽ sử dụng Local Upload hoặc FTP Upload
*/
class CDataXmlStore extends CXmlStore
{
	function __construct($file_name, $root_query = '')
	{
		parent::__construct($file_name, $root_query);
	}
	function select_all($table, $conditions = false, $options = array())
	{
		if($table === false and !$this->loaded)
		{
			if($file = CTable('file')->fields('id, php_array, data')->select($this->file_name, $options) and (!empty($file['php_array']) or !empty($file['data']))){				
				if(!$result = $file['data']){
					eval('$result = '.$file['php_array'].';');
					$result = serialize($result);
					CTable('file')->update(array('data' => $result), $this->file_name);
				}
				$result = unserialize($result);
				/*if($result)	$result = unserialize($result);
				else $result = array();*/
				//eval('$result = '.$file['php_array'].';');
				return $result;				
			}
		}
		return parent::select_all($table, $conditions, $options);
	}
	function save()
	{
		if($this->file_name)
		{

			if(portal()->setting('data-xml') == 'database')
			{
				$dataPhpArray = $this->xml->decode();
				$dataFile = serialize($dataPhpArray);
				$sDataPhpArray = var_export($dataPhpArray, 1);
				if($file = CTable('file')->fields('id')->select($this->file_name)){
					CTable('file')->update(array(
						'content' => $this->xml->doc->saveXML(),
						//'php_array' => var_export($this->xml->decode(),1),
						'php_array' => $sDataPhpArray,
						'data' => $dataFile
					), $this->file_name);
				}else{					
					CTable('file')->insert(array(
						'id'=>$this->file_name,
						'content' => $this->xml->doc->saveXML(),
						//'php_array' => var_export($this->xml->decode(),1),
						'php_array' => $sDataPhpArray,
						'data'	=> $dataFile
					));
				}
			}
			else
			{
				$this->xml->save((USE_FTP?FTP_PREFIX:'').$this->file_name);
			}
		}
	}
	function load()
	{
		if($this->file_name)
		{
			if(portal()->setting('data-xml') == 'database')
			{
				if($file = CTable('file')->fields('id, content')->select($this->file_name))
				{
					$this->xml->doc->loadXML($file['content']);
				}
				else
				{
					$file_name = str_replace(ROOT_DIR,'',str_replace('%5C','/',$this->file_name));
					if(CFile::exists($file_name))
					{
						$this->xml->doc->load($file_name);
						$dataPhpArray = $this->xml->decode();
						$dataFile = serialize($dataPhpArray);
						$sDataPhpArray = var_export($dataPhpArray, 1);
						
						CTable('file')->insert(array(
							'id'=>$this->file_name,
							'content' => $this->xml->doc->saveXML(),
							'php_array' => $sDataPhpArray,
							'data' => $dataFile
						));
					}
				}
			}
			else
			{
				if(!$this->loaded)
				{
					$this->xml->load((USE_FTP?FTP_PREFIX:'').$this->file_name);
					$this->loaded = true;
				}
			}
		}
	}
}
/**
 * Tạo đối tượng của lớp CXmlStore
 * @param $file_name string Tên file
 * @param $root_query string Tên của nút gốc
 * @return Đối tượng CXmlStore
 */
function CDataXmlStore($file_name, $root_query = '') {
	return new CDataXmlStore($file_name, $root_query);
}
?>